﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarHelpDesk.Web.Utils;

namespace CarHelpDesk.Web.Models.Repositories.LinqRepositories
{
    public class TagRepository: ITagRepository
    {
        private LinqModelDataContext _db;

        public TagRepository()
        {
            _db = new LinqModelDataContext();
#if DEBUG
            _db.Log = DebugTextWriter.Instance;
#endif
        }

        public TagRepository(bool isTest)
        {
            if (isTest)
                _db = new LinqModelDataContext(CarHelpDesk.Web.Models.Properties.Settings.Default.db_helpdesk_testConnectionString);
            else
                _db = new LinqModelDataContext();
#if DEBUG
            _db.Log = DebugTextWriter.Instance;
#endif
        }

        #region ITagRepository Members

        public IQueryable<Tag> GetTags()
        {
            return from p in _db.Tags
                   orderby p.Title
                   select p;
        }

        public void CreateTags(Tag tag)
        {
            _db.Tags.InsertOnSubmit(tag);
            _db.SubmitChanges();
        }

        public bool HasAllTagName(string[] names, out int[] ids)
        {
            List<int> idsTemp = new List<int>(); ids = idsTemp.ToArray();
            foreach (string name in names)
            {
                var v = from p in _db.Tags
                        where p.Title.ToLower() == name.ToLower()
                        select p.ID;
                if (v.Count() != 1) return false;
                else
                    idsTemp.Add(v.First());
                
            }
            ids = idsTemp.Distinct().ToArray();
            return true;
        }

        public IQueryable<Tag> GetTagsByQuestion(int id)
        {

            return (from p in _db.QuestionTags
                   where p.QuestionID == id
                   orderby p.Tag.Title
                   select p).Select(x => x.Tag);
        }

        public void AddQuestionTags(int[] tagIds, Question qu )
        {
            _db.QuestionTags.DeleteAllOnSubmit(_db.QuestionTags.Where(x => x.QuestionID == qu.ID));
            foreach (int tagId in tagIds)
            {
                QuestionTag qt = new QuestionTag();
                qt.QuestionID = qu.ID;
                qt.TagID = tagId;
                qt.CreateBy = qu.AuthorID;
                qt.CreateDate = DateTime.Now;

                _db.QuestionTags.InsertOnSubmit(qt);
                
            }
            _db.SubmitChanges();
        }

        public void EditQuestionTags(int[] tagIds, int questionID, Guid EditorID)
        {
            _db.QuestionTags.DeleteAllOnSubmit(_db.QuestionTags.Where(x => x.QuestionID == questionID));
            foreach (int tagId in tagIds)
            {
                QuestionTag qt = new QuestionTag();
                qt.QuestionID = questionID;
                qt.TagID = tagId;
                qt.CreateBy = EditorID;
                qt.CreateDate = DateTime.Now;

                _db.QuestionTags.InsertOnSubmit(qt);

            }
            _db.SubmitChanges();
        }

        public Tag GetTagByID(int id)
        {
            var v = from p in _db.Tags
                    where p.ID == id
                    select p;
            if (v.Count() == 1) return v.First();
            return null;
        }

        public void Delete(Tag tag)
        {
            _db.Tags.DeleteOnSubmit(tag);
            _db.SubmitChanges();
        }



        #endregion
    }
}
